<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
    
  <meta name="description" content="融合智慧 共创未来 冠军" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    为满足前端要求，使用 HandlerMethodReturnValueHandler 包装响应，打死不改代码风格 |  那微微的星火 是希望
  </title>
  <meta name="generator" content="hexo-theme-yilia-plus">
  
  <link rel="shortcut icon" href="/favicon.png" />
  
  <link rel="stylesheet" href="/css/style.css">
  <script src="/js/pace.min.js"></script>

  

  

</head>

</html>

<body>
  <div id="app">
    <main class="content">
      <section class="outer">
  <article id="post-为满足前端要求，使用HandlerMethodReturnValueHandler包装响应，誓死不改代码风格" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  为满足前端要求，使用 HandlerMethodReturnValueHandler 包装响应，打死不改代码风格
</h1>
  

    </header>
    

    
    <div class="article-meta">
      <a href="/2021/04/15/为满足前端要求，使用HandlerMethodReturnValueHandler包装响应，誓死不改代码风格/" class="article-date">
  <time datetime="2021-04-15T05:47:31.000Z" itemprop="datePublished">2021-04-15</time>
</a>
      
  <div class="article-category">
    <a class="article-category-link" href="/categories/Spring/">Spring</a>
  </div>

      
      
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">999字</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">4分钟</span>
        </span>
    </span>
</div>

      
    </div>
    

    
    
    <div class="tocbot"></div>





    

    
    <div class="article-entry" itemprop="articleBody">
      


      

      
      <h2 id="问题"><a href="#问题" class="headerlink" title="问题"></a>问题</h2><ul>
<li>前端一直不太认同http status(状态码)表达操作成功与否的方式，强烈要求所有接口都返回200，在响应体中增加code来表示操作是否成功。</li>
</ul>
<a id="more"></a>
<p>s<br>  比如当使用POST方式请求接口/api/user/2 时<br>  当没有操作权限时两种响应<br>  请求接口: /api/user/2<br>  请求方法: POST<br>  请求体：……(json格式的参数)</p>
<p>  原响应格式:<br>  http status: 403<br>  响应体:</p>
  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    msg: &apos;没有操作权限&apos;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>  前端想要的响应格式：<br>  http status: 200<br>  响应体:<br>  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    code: 403,</span><br><span class="line">    msg: &apos;没有操作权限&apos;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="办法"><a href="#办法" class="headerlink" title="办法"></a>办法</h2><h3 id="1-修改每个接口的返回值"><a href="#1-修改每个接口的返回值" class="headerlink" title="1. 修改每个接口的返回值"></a>1. 修改每个接口的返回值</h3><p>最简单粗暴的方式：定义一个类如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">@Data</span><br><span class="line">public class Res&#123;</span><br><span class="line">    private Integer code;</span><br><span class="line">    private String msg;</span><br><span class="line">    private Object content;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>将每个service的返回值都set给content，set相应的code和msg，然后controller将该对象返回给前端。</p>
<p>也可以每个接口都返回JSONObject的实例json</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">@GetMapping(&quot;/&#123;id&#125;&quot;)</span><br><span class="line">public JSONObject getOne((@PathVariable Integer id)&#123;</span><br><span class="line">    Person person = personService.findById(id);</span><br><span class="line"></span><br><span class="line">    JSONObject json = new JSONObject();</span><br><span class="line">    json.put(&quot;code&quot;, 200);</span><br><span class="line">    json.put(&quot;msg&quot;, &quot;成功&quot;);</span><br><span class="line">    json.put(&quot;content&quot;, person);</span><br><span class="line">    return json;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>功能完成，但是看到满屏的Res或者JSONObject操作，这么多重复代码，实在不应该。</p>
<h2 id="2-改进，修改SpringBoot原有流程"><a href="#2-改进，修改SpringBoot原有流程" class="headerlink" title="2. 改进，修改SpringBoot原有流程"></a>2. 改进，修改SpringBoot原有流程</h2><p>SpringBoot的请求处理过程如下：</p>
<p><img src="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/3ab22ddd60ff4fbeaa5287014fc7d4ff~tplv-k3u1fbpfcp-watermark.image" alt="882980-20170729131340722-226542134.png"><br>请求处理大致流程<br><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/c021a6f12ac0401789c80630b0c216f3~tplv-k3u1fbpfcp-watermark.image" alt="4702716-793d55323dd8cd66.jpg"><br>请求处理具体流程</p>
<p>在第二张图中可以看到HandlerMethodReturnValueHandler在处理所有响应的内容，只要干预他的处理过程就能将原有数据包装起来<br>下面是该类的源码<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">public interface HandlerMethodReturnValueHandler &#123;</span><br><span class="line"></span><br><span class="line">	/**</span><br><span class="line">	 * Whether the given &#123;@linkplain MethodParameter method return type&#125; is</span><br><span class="line">	 * supported by this handler.</span><br><span class="line">	 * @param returnType the method return type to check</span><br><span class="line">	 * @return &#123;@code true&#125; if this handler supports the supplied return type;</span><br><span class="line">	 * &#123;@code false&#125; otherwise</span><br><span class="line">	 */</span><br><span class="line">	boolean supportsReturnType(MethodParameter returnType);</span><br><span class="line"></span><br><span class="line">	/**</span><br><span class="line">	 * Handle the given return value by adding attributes to the model and</span><br><span class="line">	 * setting a view or setting the</span><br><span class="line">	 * &#123;@link ModelAndViewContainer#setRequestHandled&#125; flag to &#123;@code true&#125;</span><br><span class="line">	 * to indicate the response has been handled directly.</span><br><span class="line">	 * @param returnValue the value returned from the handler method</span><br><span class="line">	 * @param returnType the type of the return value. This type must have</span><br><span class="line">	 * previously been passed to &#123;@link #supportsReturnType&#125; which must</span><br><span class="line">	 * have returned &#123;@code true&#125;.</span><br><span class="line">	 * @param mavContainer the ModelAndViewContainer for the current request</span><br><span class="line">	 * @param webRequest the current request</span><br><span class="line">	 * @throws Exception if the return value handling results in an error</span><br><span class="line">	 */</span><br><span class="line">	void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType,</span><br><span class="line">			ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>实现该类的两个方法就能自定义处理过程。</p>
<ul>
<li><p>首先还是要定义一个工具类：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line">public class Res &#123;</span><br><span class="line"></span><br><span class="line">//    protected final static Res OK = new Res( HttpStatus.OK.value(),&quot;成功&quot;,&quot;&quot;);</span><br><span class="line"></span><br><span class="line">    private Integer code;</span><br><span class="line">    private String msg;</span><br><span class="line">    private String message;</span><br><span class="line">    /**</span><br><span class="line">     * 返回正常信息时，将数据放入该字段</span><br><span class="line">     */</span><br><span class="line">    private Object content;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 成功响应并需要返回数据时，使用该构造方法</span><br><span class="line">     * @param data</span><br><span class="line">     */</span><br><span class="line">    protected Res(Object data) &#123;</span><br><span class="line">        this.code = 200;</span><br><span class="line">        this.msg = &quot;成功&quot;;</span><br><span class="line">        this.message = &quot;success&quot;;</span><br><span class="line">        this.content = data;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    Res(Integer code,String msg, String message)&#123;</span><br><span class="line">        this.msg = msg;</span><br><span class="line">        this.code = code;</span><br><span class="line">        this.message = message;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public Integer getCode() &#123;</span><br><span class="line">        return code;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public String getMsg() &#123;</span><br><span class="line">        return msg;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public String getMessage() &#123;</span><br><span class="line">        return message;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public Object getContent() &#123;</span><br><span class="line">        return content;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>然后实现 HandlerMethodReturnValueHandler </p>
</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">public class WrapReturnValueHandler implements HandlerMethodReturnValueHandler &#123;</span><br><span class="line"></span><br><span class="line">    private Res ok = new Res(null);</span><br><span class="line"></span><br><span class="line">    private RequestResponseBodyMethodProcessor target;</span><br><span class="line"></span><br><span class="line">    public WrapReturnValueHandler(RequestResponseBodyMethodProcessor target) &#123;</span><br><span class="line">        this.target = target;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    public boolean supportsReturnType(MethodParameter methodParameter) &#123;</span><br><span class="line">        return true;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    public void handleReturnValue(Object o, MethodParameter methodParameter,</span><br><span class="line">                                  ModelAndViewContainer modelAndViewContainer,</span><br><span class="line">                                  NativeWebRequest nativeWebRequest) throws Exception &#123;</span><br><span class="line">        Executable executable = methodParameter.getExecutable();</span><br><span class="line"></span><br><span class="line">        Method method = (Method) executable;</span><br><span class="line">        Class&lt;?&gt; returnType = method.getReturnType();</span><br><span class="line">        if (returnType.equals(Void.class)) &#123;</span><br><span class="line">            target.handleReturnValue(ok, methodParameter, modelAndViewContainer, nativeWebRequest);</span><br><span class="line">        &#125; else if ( returnType.equals( Res.class )) &#123;</span><br><span class="line">            target.handleReturnValue(o, methodParameter, modelAndViewContainer, nativeWebRequest);</span><br><span class="line">         else &#123;</span><br><span class="line">            target.handleReturnValue(new Res(o), methodParameter, modelAndViewContainer, nativeWebRequest);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>最后将这个实现类加入到SpringBoot流程中</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">@Configuration</span><br><span class="line">public class InitializingAdvice implements InitializingBean &#123;</span><br><span class="line"></span><br><span class="line">    private final static Logger log = LoggerFactory.getLogger( InitializingAdvice.class );</span><br><span class="line"></span><br><span class="line">    @Resource</span><br><span class="line">    private RequestMappingHandlerAdapter adapter;</span><br><span class="line">    </span><br><span class="line">    @Override</span><br><span class="line">    public void afterPropertiesSet() &#123;</span><br><span class="line">        List&lt;HandlerMethodReturnValueHandler&gt; returnValueHandlers = adapter.getReturnValueHandlers();</span><br><span class="line">        List&lt;HandlerMethodReturnValueHandler&gt; handlers = new ArrayList(returnValueHandlers);</span><br><span class="line">        this.decorateHandlers(handlers);</span><br><span class="line">        adapter.setReturnValueHandlers(handlers);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    private void decorateHandlers(List&lt;HandlerMethodReturnValueHandler&gt; handlers) &#123;</span><br><span class="line">        for (HandlerMethodReturnValueHandler handler : handlers) &#123;</span><br><span class="line">            if (handler instanceof RequestResponseBodyMethodProcessor) &#123;</span><br><span class="line">                WrapReturnValueHandler decorator = new WrapReturnValueHandler(</span><br><span class="line">                        (RequestResponseBodyMethodProcessor) handler);</span><br><span class="line">                int index = handlers.indexOf(handler);</span><br><span class="line">                handlers.set(index, decorator);</span><br><span class="line">                break;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这样所有的响应在转化为流之前就能将内容包装成预期格式<br>当然抛出异常可以使用注解@ControllerAdvice捕获控制，以控制http status，然后主动将响应构造成Res实例</p>

      
      <!-- reward -->
      
      <div id="reward-btn">
        打赏
      </div>
      
    </div>
    
    
      <!-- copyright -->
      
        <div class="declare">
          <ul class="post-copyright">
            <li>
              <i class="ri-copyright-line"></i>
              <strong>版权声明： </strong s>
              本博客所有文章除特别声明外，均采用 <a href="https://www.apache.org/licenses/LICENSE-2.0.html" rel="external nofollow"
                target="_blank">Apache License 2.0</a> 许可协议。转载请注明出处！
            </li>
          </ul>
        </div>
        
    <footer class="article-footer">
      
          
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=https://github.rongzhiweilai.com/2021/04/15/为满足前端要求，使用HandlerMethodReturnValueHandler包装响应，誓死不改代码风格/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/SpringBoot/">SpringBoot</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/统一响应/">统一响应</a></li></ul>


    </footer>

  </div>

  
  
  <nav class="article-nav">
    
    
      <a href="/2020/07/15/DispatcherServlet中的两个设计模式（个人理解）/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">DispatcherServlet中的两个设计模式（个人理解）</div>
      </a>
    
  </nav>


  

  
  
<!-- valine评论 -->
<div id="vcomments-box">
    <div id="vcomments">
    </div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='https://cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>
<script>
    new Valine({
        el: '#vcomments',
        app_id: 'JveATfmbLmPTFMV2wX8uRJ0E-gzGzoHsz',
        app_key: 'XHU8pW2kSFKzbLKFc45CP5QT',
        path: window.location.pathname,
        notify: 'false',
        verify: 'false',
        avatar: 'mp',
        placeholder: '给我的文章加点评论吧~',
        recordIP: true
    });
    const infoEle = document.querySelector('#vcomments .info');
    if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
        infoEle.childNodes.forEach(function (item) {
            item.parentNode.removeChild(item);
        });
    }
</script>
<style>
    #vcomments-box {
        padding: 5px 30px;
    }

    @media screen and (max-width: 800px) {
        #vcomments-box {
            padding: 5px 0px;
        }
    }

    #vcomments-box #vcomments {
        background-color: #fff;
    }

    .v .vlist .vcard .vh {
        padding-right: 20px;
    }

    .v .vlist .vcard {
        padding-left: 10px;
    }
</style>

  

  
  
  

</article>
</section>
      <footer class="footer">
  <div class="outer">
    <ul class="list-inline">
      <li>
        &copy;
        2015-2021
        AChampion
      </li>
      <li>
        
          Powered by
        
        
        <a href="https://hexo.io" target="_blank">Hexo</a> Theme <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul class="list-inline">
      <li>
        
        
        <span>
  <i>PV:<span id="busuanzi_value_page_pv"></span></i>
  <i>UV:<span id="busuanzi_value_site_uv"></span></i>
</span>
        
      </li>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>
    <div class="to_top">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>
      </div>
    </main>
      <aside class="sidebar">
        <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="那微微的星火 是希望"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">目录</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
      </aside>
      <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>开玩笑的~不用打赏</p>
  <div class="reward-box">
    
    
  </div>
</div>
      <script src="/js/jquery-2.0.3.min.js"></script>
<script src="/js/jquery.justifiedGallery.min.js"></script>
<script src="/js/lazyload.min.js"></script>
<script src="/js/busuanzi-2.3.pure.min.js"></script>
<script src="/js/share.js"></script>

<script src="/fancybox/jquery.fancybox.min.js"></script>



<script>
  try {
    var typed = new Typed("#subtitle", {
    strings: ['综合型创新是易实现 容易把握的','相信希望的力量','打不死你的，终将使你强大'],
    startDelay: 0,
    typeSpeed: 200,
    loop: true,
    backSpeed: 100,
    showCursor: true
    });
  } catch (err) {
  }
  
</script>



<script src="/js/tocbot.min.js"></script>
<script>
  // Tocbot_v4.7.0  http://tscanlin.github.io/tocbot/
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer:'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto',
    onClick: (e) => {
      $('.toc-link').removeClass('is-active-link');
      $(`a[href=${e.target.hash}]`).addClass('is-active-link');
      $(e.target.hash).scrollIntoView();
      return false;
    }
  });
</script>


<script>
  var ayerConfig = {
    mathjax: false
  }
</script>

<script src="/js/ayer.js"></script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>




<script type="text/javascript" src="https://js.users.51.la/20544303.js"></script>
  </div>
</body>

</html>